<?php
/**
 * 获取订单数据写入数据库
 * Created by wzb.
 * User: wzb
 * time:2017/08/12
 */


require("interface.php");

$app_info = "select * from dlz_account_info where status='1'";
$query = $db->query($app_info);//返回影响了多少行数据
echo "***********Get account info********************\n";
while($row = $query->fetch()){
        $id_m = $row['id'];
        $store_name_m = $row['store_name'];
        $token_m = $row['token'];
        $baseUrl_m = $row['baseUrl'];
        $username_m = $row['username'];
        $password_m = $row['password'];
        echo "//////////////$store_name_m/////////////////////\n";
        $Mazentop = new Mazentop($token_m,$baseUrl_m,$username_m,$password_m);
        Getproduction_optionid($Mazentop,$db,$id_m);
  }


function Getproduction_optionid($Mazentop,$db,$id_m){
      $item_info = "select * from dlz_attribute_all_id where store_id='$id_m' and options_id is null";
      $query_item = $db->query($item_info);//返回影响了多少行数据
      while($rows = $query_item->fetch()){
        $item_id = $rows['item_id'];
        $increment_id = $rows['increment_id'];
        echo "------------------ $item_id------------------------\n";
        $product_data = $Mazentop->getproductAttribute($item_id = "$item_id");

      echo "*************$id_m*****Start GET option id****************************\n";
       /**获取产品属性信息开始*/
      $created_at = $product_data['created_at'];//产品下单时间
      $item_id = $product_data['item_id'];//
      $name = $product_data['name'];//
      $order_id = $product_data['order_id'];//
      $price = $product_data['price'];//产品价格
      $original_price = $product_data['original_price'];//基础价格
      $product_type = $product_data['product_type'];//产品组合类型
      $qty_ordered = $product_data['qty_ordered'];//订单数
      $sku = $product_data['sku'];//Sku
      $store_id = $product_data['store_id'];//商店id
      $updated_at = $product_data['updated_at'];//更新时间
      $product_option = $product_data['product_option']['extension_attributes']['custom_options'];//产品属性  如果不存在 有错误，不影响，先不改动
      $option_arrary = array();
      if(is_array($product_option)){
      foreach ($product_option as $key => $value) {
                $option_id = $value['option_id'];
                $option_value = $value['option_value'];
                $option_arrary["$option_id"] = $option_value;
      }
            }
      $options_id = json_encode($option_arrary);
      $insert_option = "update dlz_attribute_all_id set options_id='$options_id',sync_status='0' where item_id='$item_id'";
      $result = $db->exec($insert_option);
      if( $result > 0){
          echo "$options_id \n";
      }

   }//while
        echo "++++++++++++++++++end++++++++++++++++++++\n"  ; 
}
$db = null;

/**
{
  "amount_refunded": 0,
  "applied_rule_ids": "22",
  "base_amount_refunded": 0,
  "base_discount_amount": 20,
  "base_discount_invoiced": 20,
  "base_discount_tax_compensation_amount": 0,
  "base_discount_tax_compensation_invoiced": 0,
  "base_original_price": 46,
  "base_price": 149.69,
  "base_price_incl_tax": 149.69,
  "base_row_invoiced": 149.69,
  "base_row_total": 149.69,
  "base_row_total_incl_tax": 149.69,
  "base_tax_amount": 0,
  "base_tax_invoiced": 0,
  "created_at": "2017-08-11 15:05:10",
  "discount_amount": 20,
  "discount_invoiced": 20,
  "discount_percent": 0,
  "free_shipping": 0,
  "discount_tax_compensation_amount": 0,
  "discount_tax_compensation_invoiced": 0,
  "is_qty_decimal": 0,
  "is_virtual": 0,
  "item_id": 445,
  "name": "Mslynn Brazilian Body Wave Hair Brazilian Virgin Hair 3 Bundles With Closure With Baby Hair",
  "no_discount": 0,
  "order_id": 384,
  "original_price": 46,
  "price": 149.69,
  "price_incl_tax": 149.69,
  "product_id": 263,
  "product_type": "simple",
  "qty_canceled": 0,
  "qty_invoiced": 1,
  "qty_ordered": 1,
  "qty_refunded": 0,
  "qty_shipped": 0,
  "quote_item_id": 5318,
  "row_invoiced": 149.69,
  "row_total": 149.69,
  "row_total_incl_tax": 149.69,
  "row_weight": 0.5,
  "sku": "BBW3P",
  "store_id": 1,
  "tax_amount": 0,
  "tax_invoiced": 0,
  "tax_percent": 0,
  "updated_at": "2017-08-11 15:05:10",
  "weee_tax_applied": "[]",
  "weight": 0.5,
  "product_option": {
    "extension_attributes": {
      "custom_options": [
        {
          "option_id": "3135",
          "option_value": "53486"
        },
        {
          "option_id": "3136",
          "option_value": "53494"
        },
        {
          "option_id": "3137",
          "option_value": "53514"
        }
      ]
    }
  }
}
Response Code
 */


//include("getorderproductExtensionAttr.php");
